package com.rk.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rk.cmn.client.DictFeignClient;
import com.rk.hosp.enums.DictEnum;
import com.rk.hosp.model.user.Patient;
import com.rk.user.mapper.PatientMapper;
import com.rk.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author :Rk.
 * @date : 2022/11/26
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    private PatientMapper patientMapper;

    @Autowired
    private DictFeignClient dictFeignClient;


    /**
     * 通过用户id去查询就诊人信息
     * @param userId
     * @return
     */
    @Override
    public List<Patient> findAllUserId(Long userId) {
        LambdaQueryWrapper<Patient> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Patient::getUserId,userId);
        List<Patient> list = patientMapper.selectList(lqw);
        //远程调用字典查询
        list.stream().forEach(
                item->{
                    this.packPatient(item);
                }
        );
        return list;
    }



    /**
     * 使用远程调用查询字典 封装医院名称和地区
     * @param patient
     * @return
     */
    private Patient packPatient(Patient patient){
        //根据证件类型编码，获取证件类型具体指
        String certificatesTypeString =
                dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());//联系人证件
        //联系人证件类型
        String contactsCertificatesTypeString =
                dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(),patient.getContactsCertificatesType());
        //省
        String provinceString = dictFeignClient.getName(patient.getProvinceCode());
        //市
        String cityString = dictFeignClient.getName(patient.getCityCode());
        //区
        String districtString = dictFeignClient.getName(patient.getDistrictCode());
        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
        return patient;
    }


    /**
     * 通过id查询
     * @param id
     * @return
     */
    @Override
    public Patient getPatientById(Long id) {
        Patient patient1 = this.getById(id);
        Patient patient2 = this.packPatient(patient1);
        return patient2;
    }



}
